// Christian Larco. This is assignment 2, I/O and interrupts.  

//------------- This are the header files that will be included in the program ------------------

#include <avr/io.h>				//This is the header file for the input and output for the AVR
#include <avr/interrupt.h>		//This is the interrupt header for the AVR
#define F_CPU 8000000UL			//This command defines the frequency/speed of the CPU
#include <util/delay.h>			//This is the header for the delays

//------------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------------


//------- This is the main body of the program which is going to turn on the 8-bit LED bar -------

int intCount = 0; // Holds how many global interrupts happened
 
int main(void)
{

	//----------------- This sets the directions and sets up the ports for the program ----------
        DDRB = 0b11111111;		//This sets the direction in which the LEDS are going to turn on
        DDRD = 0b11000000;		//This sets bit 7 to turn on every 40 cycles and bit 6 to 
								//turn on every 10 cycles.
        PORTD = 0b00000000;		//Clears PortD
		PORTB = 0b00000000;		//Clears PortB
	//-------------------------------------------------------------------------------------------
		
	//----------------- This are the caruables and the commands used in the program -------------
        int yayisten = 0;		//Counter to get the ten count and turn on the light
        int counter = 0;		//This is used to get the counter of the whole program
        EICRA = 0b00000010;		//This is the initializer for the falling edge interrupt
        EIMSK=1;				
        sei();					//This command enables the interrupt	
	//--------------------------------------------------------------------------------------------
		
	
        while(1)
        {
                _delay_ms(250);		//Command to set up the delay of 0.25 sec so 250ms
                counter++;			//This increments the total global counter
                PORTB = counter;	//This command updates the PORTB and sets it equal to the 
									//number of the counter
                yayisten++;			//Increments counter of 10 by one
                if(yayisten==10)	//This is the loop to check the if the counter is ten
				{	
					yayisten = 0;		//This resets the counter to zero to count then 
										//next set to zero
                    PORTD ^= 1 << 6;	//This toggles bit number 9 of the LED bar
                }
                _delay_ms(250);		//Command to set up the delay of 0.25 sec so 250ms
        }
		
}
//-----------------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------------


//---------- Code for the interrupt, It catches the interrupt on every falling edge -------------
//It is set to happen on every ten cycles so since we are only catching the falling edge we are 
//only going to get it on every ten count.

ISR(INT0_vect){
	intCount++;			//This updates how many falling edges happen
	
	if(intCount==2){		//This catches the falling edge and then resets the counter
		PORTD ^= 1 << 7;	//Toggles the seventh bit on and off when falling edge occurs
		intCount = 0;		//This sets the counter of interrupts back to zero
	}
}

//-----------------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------------
 
